const db = require('../common/db');
const sql = require('../common/sql');
const tools = require('../common/tools');

module.exports = {
    addOrder: (req, res) => {
        let { orderDate, goodsList, total } = req.body;
        let ordernum = tools.generateUUID();
        if (goodsList.length > 0) {
            let userid = goodsList[0].userid;
            db.query(sql.orders.addOrder, [ordernum, userid, orderDate, total], (err, results) => {
                if (err) {
                    res.send({ code: 500, msg: err.message });
                } else {
                    let orderid = results.insertId;
                    let sqlVparams = [];
                    goodsList.forEach(item => {
                        let insert = {
                            sql: sql.orders.addDetail,
                            params: [orderid, item.gid, item.price, item.num]
                        }
                        sqlVparams.push(insert);
                        let del = {
                            sql: sql.carts.delById,
                            params: [item.cartid]
                        }
                        sqlVparams.push(del);
                    });
                    db.dbTransaction(sqlVparams, (err, results) => {
                        if (err) {
                            res.send({ code: 500, msg: err.message });
                        } else {
                            res.send({ code: 200 });
                        }
                    })
                }
            })
        } else {
            res.send({ code: 201, msg: '没有商品信息，不能生成订单！' });
        }
    },
    ordersByU: (req, res) => {
        let userid = req.body.userid;
        db.query(sql.orders.ordersByU + sql.orders.detailsByU, [userid, userid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                let orders = []
                results[0].forEach(item => {
                    item.details = results[1].filter((d) => d.orderid === item.orderid);
                    orders.push(item);
                });
                res.send({ code: 200, orders: orders })
            }
        })
    },
     order: (req, res) => {
        let userid = req.body.userid;
        db.query(sql.orders.order, [userid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, order: results })
            }
        })
    },
    updateorder: (req, res) => {
        const { orderid, ordernum, userid, total } = req.body;
        db.query(
            sql.orders.updateorder,
            [ordernum, userid, total, orderid],
            (err, results) => {
                if (err) {
                    console.error('数据库错误:', err);
                    res.status(500).send({ code: 500, msg: '更新失败' });
                } else {
                    if (results.affectedRows > 0) {
                        res.send({ code: 200, msg: '更新成功' });
                    } else {
                        res.send({ code: 404, msg: '未找到对应订单' });
                    }
                }
            }
        );
    },
    jiaorder: (req, res) => {
        const { orderid, ordernum, userid,createtime,total,status } = req.body;
        db.query(sql.orders.jiaorder,[ordernum, userid,createtime, total,status, orderid],
            (err, results) => {
                if (err) {
                    res.send({ code: 500, msg: err.message });
                } else {
                    res.send({ code: 200, order:results  });
                }
            })
    },
    delorder: (req, res) => {
        let { orderid } = req.body;
        db.query(sql.orders.delorder, [orderid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, order: results});
            }
        })
    }
}